package cn.akwangl.concurrency.web.mybatis;

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.builder.ConfigBuilder;
import com.baomidou.mybatisplus.generator.config.converts.MySqlTypeConvert;
import com.baomidou.mybatisplus.generator.config.po.LikeTable;
import com.baomidou.mybatisplus.generator.config.querys.MySqlQuery;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import com.baomidou.mybatisplus.generator.fill.Column;
import com.baomidou.mybatisplus.generator.fill.Property;
import com.baomidou.mybatisplus.generator.keywords.MySqlKeyWordsHandler;

import java.util.Collections;

/**
 * mybatis-plus 代码生成器.
 *
 * @author 阿坤同学
 * @date 2022-11-22 9:52
 */
public class MybatisPlusGenerator {

    public static final String OUT_PUT_DIR = "C:\\Users\\akwangl\\Downloads\\java";
    public static final String MAPPER_OUT_PUT_DIR = OUT_PUT_DIR + "\\mapper\\";


    public static void main(String[] args) {
        // 生产多个表请用“,”间隔
        String tableName = "carousel";
        String moduleName = "index";

        ConfigBuilder configBuilder = new ConfigBuilder(createPackageConfig(moduleName), createDataSourceConfig(), createStrategyConfig(tableName)
                , createTemplateConfig(), createGlobalConfig(), null);
        AutoGenerator generator = new AutoGenerator(createDataSourceConfig());
        generator.config(configBuilder);
        generator.execute(new FreemarkerTemplateEngine());
    }



    public static DataSourceConfig createDataSourceConfig() {
        return new DataSourceConfig.Builder("jdbc:mysql://127.0.0.1:3306/foodie-shop-dev?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8",
                "root", "root")
                .dbQuery(new MySqlQuery())
                .schema("mybatis-plus")
                .typeConvert(new MySqlTypeConvert())
                .keyWordsHandler(new MySqlKeyWordsHandler())
                .build();
    }

    public static GlobalConfig createGlobalConfig() {
        return new GlobalConfig.Builder()
                .outputDir(OUT_PUT_DIR)
                .author("阿坤同学")
                .dateType(DateType.TIME_PACK)
                .commentDate("yyyy-MM-dd HH:mm:ss")
                .build();
    }

    public static PackageConfig createPackageConfig(String moduleName) {
        return new PackageConfig.Builder()
                .parent("cn.akwangl.concurrency.web.model")
                .moduleName(moduleName)
                .entity("pojo")
                .service("service")
                .serviceImpl("service.impl")
                .mapper("mapper")
                .xml("mapper.xml")
                .controller("controller")
                .pathInfo(Collections.singletonMap(OutputFile.xml, MAPPER_OUT_PUT_DIR + moduleName))
                .build();
    }

    public static TemplateConfig createTemplateConfig() {
        return new TemplateConfig.Builder()
                .disable(TemplateType.ENTITY)
                .entity("/templates/entity.java")
                .service("/templates/service.java")
                .serviceImpl("/templates/serviceImpl.java")
                .mapper("/templates/mapper.java")
                .xml("/templates/mapper.xml")
                .controller("/templates/controller.java")
                .build();
    }

    public static StrategyConfig createStrategyConfig(String tableName) {
        StrategyConfig strategyConfig = new StrategyConfig.Builder()
                .enableCapitalMode()
                .enableSkipView()
                .disableSqlFilter()
                .likeTable(new LikeTable(tableName))
                .addInclude(tableName)
                .addTablePrefix("t_", "c_")
                .build();

        // 实体
        strategyConfig.entityBuilder()
                .disableSerialVersionUID()
                .enableLombok()
                .enableTableFieldAnnotation()
                .enableActiveRecord()
                .logicDeleteColumnName("is_delete")
                .logicDeletePropertyName("isDelete")
                .naming(NamingStrategy.underline_to_camel)
                .columnNaming(NamingStrategy.underline_to_camel)
                .addTableFills(new Column("createdTime", FieldFill.INSERT))
                .addTableFills(new Property("updatedTime", FieldFill.INSERT_UPDATE))
                .idType(IdType.ASSIGN_ID)
                .formatFileName("%sEntity")
                .build();

        // mapper
        strategyConfig.mapperBuilder()
                .enableMapperAnnotation()
                .enableBaseResultMap()
                .enableBaseColumnList()
                .formatMapperFileName("%sMapper")
                .formatXmlFileName("%sMapper")
                .build();

        // service
        strategyConfig.serviceBuilder()
                .formatServiceFileName("%sService")
                .formatServiceImplFileName("%sServiceImpl")
                .build();

        // controller
        strategyConfig.controllerBuilder()
                .enableHyphenStyle()
                .enableRestStyle()
                .formatFileName("%Controller")
                .build();
        return strategyConfig;
    }
}
